/***********************************************************************
 *            Project: CoreCms
 *        ProjectName: 核心内容管理系统                                
 *                Web: https://www.corecms.net                      
 *             Author: 大灰灰                                          
 *              Email: jianweie@163.com                                
 *         CreateTime: 2025/3/5 0:03:24
 *        Description: 暂无
 ***********************************************************************/


using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreCms.Net.Configuration;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Filter;
using CoreCms.Net.Loging;
using CoreCms.Net.IServices;
using CoreCms.Net.Utility.Helper;
using CoreCms.Net.Utility.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using NPOI.HSSF.UserModel;
using SqlSugar;
using CoreCms.Net.Web.Admin.Infrastructure;

namespace CoreCms.Net.Web.Admin.Controllers
{
    /// <summary>
    /// 渠道表
    ///</summary>
    [Description("渠道表")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    [RequiredErrorForAdmin]
    [Authorize(Permissions.Name)]
    public class CoreCmsChannelController : ControllerBase
    {
        private readonly IWebHostEnvironment _webHostEnvironment;
        private readonly ICoreCmsChannelServices _CoreCmsChannelServices;
        private readonly ICoreCmsplatformServices _CoreCmsplatformServices;

        /// <summary>
        /// 构造函数
        ///</summary>
        public CoreCmsChannelController(IWebHostEnvironment webHostEnvironment
            ,ICoreCmsChannelServices CoreCmsChannelServices,ICoreCmsplatformServices coreCmsplatformServices
            )
        {
            _webHostEnvironment = webHostEnvironment;
            _CoreCmsChannelServices = CoreCmsChannelServices;
            _CoreCmsplatformServices= coreCmsplatformServices;
        }

        #region 获取列表============================================================
        // POST: Api/CoreCmsChannel/GetPageList
         /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取列表")]
        public async Task<AdminUiCallBack> GetPageList()
        {
            var jm = new AdminUiCallBack();
            var pageCurrent = Request.Form["page"].FirstOrDefault().ObjectToInt(1);
            var pageSize = Request.Form["limit"].FirstOrDefault().ObjectToInt(30);
            var where = PredicateBuilder.True<CoreCmsChannel>();
            //获取排序字段
            var orderField = Request.Form["orderField"].FirstOrDefault();

            Expression<Func<CoreCmsChannel, object>> orderEx = orderField switch
            {
                "id" => p => p.id,"name" => p => p.name,"method" => p => p.method,"app_id" => p => p.app_id,"sign" => p => p.sign,"secret" => p => p.secret,"createtime" => p => p.createtime,"create_time_start" => p => p.create_time_start,"create_time_end" => p => p.create_time_end,"update_time_start" => p => p.update_time_start,"update_time_end" => p => p.update_time_end,"status" => p => p.status,"have_after_sale" => p => p.have_after_sale,"next_id" => p => p.next_id,"limit" => p => p.limit,"platformId" => p => p.platformId,
                _ => p => p.id
            };

            //设置排序方式
            var orderDirection = Request.Form["orderDirection"].FirstOrDefault();
            var orderBy = orderDirection switch
            {
                "asc" => OrderByType.Asc,
                "desc" => OrderByType.Desc,
                _ => OrderByType.Desc
            };
            //查询筛选
			
			//id int
			var id = Request.Form["id"].FirstOrDefault().ObjectToInt(0);
            if (id > 0)
            {
                where = where.And(p => p.id == id);
            }
            where = where.And(p =>true);
			//渠道名字 varchar
			var name = Request.Form["name"].FirstOrDefault();
            if (!string.IsNullOrEmpty(name))
            {
                where = where.And(p => p.name.Contains(name));
            }
			//接口 varchar
			var method = Request.Form["method"].FirstOrDefault();
            if (!string.IsNullOrEmpty(method))
            {
                where = where.And(p => p.method.Contains(method));
            }
			//应用ID varchar
			var app_id = Request.Form["app_id"].FirstOrDefault();
            if (!string.IsNullOrEmpty(app_id))
            {
                where = where.And(p => p.app_id.Contains(app_id));
            }
			//sign varchar
			var sign = Request.Form["sign"].FirstOrDefault();
            if (!string.IsNullOrEmpty(sign))
            {
                where = where.And(p => p.sign.Contains(sign));
            }
			//secret varchar
			var secret = Request.Form["secret"].FirstOrDefault();
            if (!string.IsNullOrEmpty(secret))
            {
                where = where.And(p => p.secret.Contains(secret));
            }
			//创建时间 datetime
			var createtime = Request.Form["createtime"].FirstOrDefault();
            if (!string.IsNullOrEmpty(createtime))
            {
                if (createtime.Contains("到"))
                {
                    var dts = createtime.Split("到");
                    var dtStart = dts[0].Trim().ObjectToDate();
                    where = where.And(p => p.createtime > dtStart);
                    var dtEnd = dts[1].Trim().ObjectToDate();
                    where = where.And(p => p.createtime < dtEnd);
                }
                else
                {
                    var dt = createtime.ObjectToDate();
                    where = where.And(p => p.createtime > dt);
                }
            }
			//订单创建起始时间 datetime
			var create_time_start = Request.Form["create_time_start"].FirstOrDefault();
            if (!string.IsNullOrEmpty(create_time_start))
            {
                if (create_time_start.Contains("到"))
                {
                    var dts = create_time_start.Split("到");
                    var dtStart = dts[0].Trim().ObjectToDate();
                    where = where.And(p => p.create_time_start > dtStart);
                    var dtEnd = dts[1].Trim().ObjectToDate();
                    where = where.And(p => p.create_time_start < dtEnd);
                }
                else
                {
                    var dt = create_time_start.ObjectToDate();
                    where = where.And(p => p.create_time_start > dt);
                }
            }
			//订单创建截止时间 datetime
			var create_time_end = Request.Form["create_time_end"].FirstOrDefault();
            if (!string.IsNullOrEmpty(create_time_end))
            {
                if (create_time_end.Contains("到"))
                {
                    var dts = create_time_end.Split("到");
                    var dtStart = dts[0].Trim().ObjectToDate();
                    where = where.And(p => p.create_time_end > dtStart);
                    var dtEnd = dts[1].Trim().ObjectToDate();
                    where = where.And(p => p.create_time_end < dtEnd);
                }
                else
                {
                    var dt = create_time_end.ObjectToDate();
                    where = where.And(p => p.create_time_end > dt);
                }
            }
			//订单更新起始时间 datetime
			var update_time_start = Request.Form["update_time_start"].FirstOrDefault();
            if (!string.IsNullOrEmpty(update_time_start))
            {
                if (update_time_start.Contains("到"))
                {
                    var dts = update_time_start.Split("到");
                    var dtStart = dts[0].Trim().ObjectToDate();
                    where = where.And(p => p.update_time_start > dtStart);
                    var dtEnd = dts[1].Trim().ObjectToDate();
                    where = where.And(p => p.update_time_start < dtEnd);
                }
                else
                {
                    var dt = update_time_start.ObjectToDate();
                    where = where.And(p => p.update_time_start > dt);
                }
            }
			//订单更新截止时间 datetime
			var update_time_end = Request.Form["update_time_end"].FirstOrDefault();
            if (!string.IsNullOrEmpty(update_time_end))
            {
                if (update_time_end.Contains("到"))
                {
                    var dts = update_time_end.Split("到");
                    var dtStart = dts[0].Trim().ObjectToDate();
                    where = where.And(p => p.update_time_end > dtStart);
                    var dtEnd = dts[1].Trim().ObjectToDate();
                    where = where.And(p => p.update_time_end < dtEnd);
                }
                else
                {
                    var dt = update_time_end.ObjectToDate();
                    where = where.And(p => p.update_time_end > dt);
                }
            }
			//订单状态 varchar
			var status = Request.Form["status"].FirstOrDefault();
            if (!string.IsNullOrEmpty(status))
            {
                where = where.And(p => p.status.Contains(status));
            }
			//是否存在售后 varchar
			var have_after_sale = Request.Form["have_after_sale"].FirstOrDefault();
            if (!string.IsNullOrEmpty(have_after_sale))
            {
                where = where.And(p => p.have_after_sale.Contains(have_after_sale));
            }
			//查询起点页数 int
			var next_id = Request.Form["next_id"].FirstOrDefault().ObjectToInt(0);
            if (next_id > 0)
            {
                where = where.And(p => p.next_id == next_id);
            }
			//条数 int
			var limit = Request.Form["limit"].FirstOrDefault().ObjectToInt(0);
            if (limit > 0)
            {
                where = where.And(p => p.limit == limit);
            }
			//平台ID int
			var platformId = Request.Form["platformId"].FirstOrDefault().ObjectToInt(0);
            if (platformId > 0)
            {
                where = where.And(p => p.platformId == platformId);
            }
            //获取数据
            var list = await _CoreCmsChannelServices.QueryPageAsync(where, orderEx, orderBy, pageCurrent, pageSize, true);
            //返回数据
            jm.data = list;
            jm.code = 0;
            jm.count = list.TotalCount;
            jm.msg = "数据调用成功!";
            return jm;
        }
        #endregion

        #region 首页数据============================================================
        // POST: Api/CoreCmsChannel/GetIndex
        /// <summary>
        /// 首页数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("首页数据")]
        public async Task<AdminUiCallBack> GetIndex()
        {
            //返回数据
            var jm = new AdminUiCallBack { code = 0 };
            var list = await _CoreCmsplatformServices.QueryAsync();
            jm.data = new {
               list
            };
            return jm;
        }
        #endregion

        #region 创建数据============================================================
        // POST: Api/CoreCmsChannel/GetCreate
        /// <summary>
        /// 创建数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("创建数据")]
        public async Task<AdminUiCallBack> GetCreate()
        {
            //返回数据
            var jm = new AdminUiCallBack { code = 0 };
            var list = await _CoreCmsplatformServices.QueryAsync();
            jm.data = new
            {
                list
            };
            return jm;
        }
        #endregion

        #region 创建提交============================================================
        // POST: Api/CoreCmsChannel/DoCreate
        /// <summary>
        /// 创建提交
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("创建提交")]
        public async Task<AdminUiCallBack> DoCreate([FromBody]CoreCmsChannel entity)
        {
            var jm = await _CoreCmsChannelServices.InsertAsync(entity);
            return jm;
        }
        #endregion

        #region 编辑数据============================================================
        // POST: Api/CoreCmsChannel/GetEdit
        /// <summary>
        /// 编辑数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("编辑数据")]
        public async Task<AdminUiCallBack> GetEdit([FromBody]FMIntId entity)
        {
            var jm = new AdminUiCallBack();

            var model = await _CoreCmsChannelServices.QueryByIdAsync(entity.id, false);
            if (model == null)
            {
                jm.msg = "不存在此信息";
                return jm;
            }
            jm.code = 0;
            jm.data = model;

            return jm;
        }
        #endregion

        #region 编辑提交============================================================
        // POST: Api/CoreCmsChannel/Edit
        /// <summary>
        /// 编辑提交
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("编辑提交")]
        public async Task<AdminUiCallBack> DoEdit([FromBody]CoreCmsChannel entity)
        {
            var jm = await _CoreCmsChannelServices.UpdateAsync(entity);
            return jm;
        }
        #endregion

        #region 删除数据============================================================
        // POST: Api/CoreCmsChannel/DoDelete/10
        /// <summary>
        /// 单选删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("单选删除")]
        public async Task<AdminUiCallBack> DoDelete([FromBody]FMIntId entity)
        {
            var jm = new AdminUiCallBack();

            var model = await _CoreCmsChannelServices.ExistsAsync(p => p.id == entity.id, true);
            if (!model)
            {
                jm.msg = GlobalConstVars.DataisNo;
				return jm;
            }
            jm = await _CoreCmsChannelServices.DeleteByIdAsync(entity.id);

            return jm;
        }
        #endregion

        #region 批量删除============================================================
        // POST: Api/CoreCmsChannel/DoBatchDelete/10,11,20
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("批量删除")]
        public async Task<AdminUiCallBack> DoBatchDelete([FromBody]FMArrayIntIds entity)
        {
            var jm = await _CoreCmsChannelServices.DeleteByIdsAsync(entity.id);
            return jm;
        }

        #endregion

        #region 预览数据============================================================
        // POST: Api/CoreCmsChannel/GetDetails/10
        /// <summary>
        /// 预览数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("预览数据")]
        public async Task<AdminUiCallBack> GetDetails([FromBody]FMIntId entity)
        {
            var jm = new AdminUiCallBack();

            var model = await _CoreCmsChannelServices.QueryByIdAsync(entity.id, false);
            if (model == null)
            {
                jm.msg = "不存在此信息";
                return jm;
            }
            jm.code = 0;
            jm.data = model;

            return jm;
        }
        #endregion

        #region 选择导出============================================================
        // POST: Api/CoreCmsChannel/SelectExportExcel/10
        /// <summary>
        /// 选择导出
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("选择导出")]
        public async Task<AdminUiCallBack> SelectExportExcel([FromBody]FMArrayIntIds entity)
        {
            var jm = new AdminUiCallBack();

            //创建Excel文件的对象
            var book = new HSSFWorkbook();
            //添加一个sheet
            var mySheet = book.CreateSheet("Sheet1");
            //获取list数据
            var listModel = await _CoreCmsChannelServices.QueryListByClauseAsync(p => entity.id.Contains(p.id), p => p.id, OrderByType.Asc, true);
            //给sheet1添加第一行的头部标题
            var headerRow = mySheet.CreateRow(0);
            var headerStyle = ExcelHelper.GetHeaderStyle(book);
            var cellNum = 0;

            var cell0 = headerRow.CreateCell(cellNum);
            cell0.SetCellValue("id");
            cell0.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;

            var cell1 = headerRow.CreateCell(cellNum);
            cell1.SetCellValue("渠道名字");
            cell1.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;

            var cell2 = headerRow.CreateCell(cellNum);
            cell2.SetCellValue("接口");
            cell2.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;

            var cell3 = headerRow.CreateCell(cellNum);
            cell3.SetCellValue("应用ID");
            cell3.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;

            var cell4 = headerRow.CreateCell(cellNum);
            cell4.SetCellValue("sign");
            cell4.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;

            var cell5 = headerRow.CreateCell(cellNum);
            cell5.SetCellValue("secret");
            cell5.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;

            var cell6 = headerRow.CreateCell(cellNum);
            cell6.SetCellValue("创建时间");
            cell6.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;

            var cell7 = headerRow.CreateCell(cellNum);
            cell7.SetCellValue("订单创建起始时间");
            cell7.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;

            var cell8 = headerRow.CreateCell(cellNum);
            cell8.SetCellValue("订单创建截止时间");
            cell8.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;

            var cell9 = headerRow.CreateCell(cellNum);
            cell9.SetCellValue("订单更新起始时间");
            cell9.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;

            var cell10 = headerRow.CreateCell(cellNum);
            cell10.SetCellValue("订单更新截止时间");
            cell10.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;

            var cell11 = headerRow.CreateCell(cellNum);
            cell11.SetCellValue("订单状态");
            cell11.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;

            var cell12 = headerRow.CreateCell(cellNum);
            cell12.SetCellValue("是否存在售后");
            cell12.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;

            var cell13 = headerRow.CreateCell(cellNum);
            cell13.SetCellValue("查询起点页数");
            cell13.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;

            var cell14 = headerRow.CreateCell(cellNum);
            cell14.SetCellValue("条数");
            cell14.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;

            var cell15 = headerRow.CreateCell(cellNum);
            cell15.SetCellValue("平台ID");
            cell15.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;

            headerRow.Height = 30 * 20;
            var commonCellStyle = ExcelHelper.GetCommonStyle(book);

            //将数据逐步写入sheet1各个行
            for (var i = 0; i < listModel.Count; i++)
            {
                var rowTemp = mySheet.CreateRow(i + 1);
                var cellNumTemp = 0;

                    var rowTemp0 = rowTemp.CreateCell(cellNumTemp);
                        rowTemp0.SetCellValue(listModel[i].id.ToString());
                        rowTemp0.CellStyle = commonCellStyle;
                        cellNumTemp++;

                    var rowTemp1 = rowTemp.CreateCell(cellNumTemp);
                        rowTemp1.SetCellValue(listModel[i].name.ToString());
                        rowTemp1.CellStyle = commonCellStyle;
                        cellNumTemp++;

                    var rowTemp2 = rowTemp.CreateCell(cellNumTemp);
                        rowTemp2.SetCellValue(listModel[i].method.ToString());
                        rowTemp2.CellStyle = commonCellStyle;
                        cellNumTemp++;

                    var rowTemp3 = rowTemp.CreateCell(cellNumTemp);
                        rowTemp3.SetCellValue(listModel[i].app_id.ToString());
                        rowTemp3.CellStyle = commonCellStyle;
                        cellNumTemp++;

                    var rowTemp4 = rowTemp.CreateCell(cellNumTemp);
                        rowTemp4.SetCellValue(listModel[i].sign.ToString());
                        rowTemp4.CellStyle = commonCellStyle;
                        cellNumTemp++;

                    var rowTemp5 = rowTemp.CreateCell(cellNumTemp);
                        rowTemp5.SetCellValue(listModel[i].secret.ToString());
                        rowTemp5.CellStyle = commonCellStyle;
                        cellNumTemp++;

                    var rowTemp6 = rowTemp.CreateCell(cellNumTemp);
                        rowTemp6.SetCellValue(listModel[i].createtime.ToString());
                        rowTemp6.CellStyle = commonCellStyle;
                        cellNumTemp++;

                    var rowTemp7 = rowTemp.CreateCell(cellNumTemp);
                        rowTemp7.SetCellValue(listModel[i].create_time_start.ToString());
                        rowTemp7.CellStyle = commonCellStyle;
                        cellNumTemp++;

                    var rowTemp8 = rowTemp.CreateCell(cellNumTemp);
                        rowTemp8.SetCellValue(listModel[i].create_time_end.ToString());
                        rowTemp8.CellStyle = commonCellStyle;
                        cellNumTemp++;

                    var rowTemp9 = rowTemp.CreateCell(cellNumTemp);
                        rowTemp9.SetCellValue(listModel[i].update_time_start.ToString());
                        rowTemp9.CellStyle = commonCellStyle;
                        cellNumTemp++;

                    var rowTemp10 = rowTemp.CreateCell(cellNumTemp);
                        rowTemp10.SetCellValue(listModel[i].update_time_end.ToString());
                        rowTemp10.CellStyle = commonCellStyle;
                        cellNumTemp++;

                    var rowTemp11 = rowTemp.CreateCell(cellNumTemp);
                        rowTemp11.SetCellValue(listModel[i].status.ToString());
                        rowTemp11.CellStyle = commonCellStyle;
                        cellNumTemp++;

                    var rowTemp12 = rowTemp.CreateCell(cellNumTemp);
                        rowTemp12.SetCellValue(listModel[i].have_after_sale.ToString());
                        rowTemp12.CellStyle = commonCellStyle;
                        cellNumTemp++;

                    var rowTemp13 = rowTemp.CreateCell(cellNumTemp);
                        rowTemp13.SetCellValue(listModel[i].next_id.ToString());
                        rowTemp13.CellStyle = commonCellStyle;
                        cellNumTemp++;

                    var rowTemp14 = rowTemp.CreateCell(cellNumTemp);
                        rowTemp14.SetCellValue(listModel[i].limit.ToString());
                        rowTemp14.CellStyle = commonCellStyle;
                        cellNumTemp++;

                    var rowTemp15 = rowTemp.CreateCell(cellNumTemp);
                        rowTemp15.SetCellValue(listModel[i].platformId.ToString());
                        rowTemp15.CellStyle = commonCellStyle;
                        cellNumTemp++;

            }
            // 导出excel
            string webRootPath = _webHostEnvironment.WebRootPath;
            string tpath = "/files/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "-CoreCmsChannel导出(选择结果).xls";
            string filePath = webRootPath + tpath;
            DirectoryInfo di = new DirectoryInfo(filePath);
            if (!di.Exists)
            {
                di.Create();
            }
            FileStream fileHssf = new FileStream(filePath + fileName, FileMode.Create);
            book.Write(fileHssf);
            fileHssf.Close();

            jm.code = 0;
            jm.msg = GlobalConstVars.ExcelExportSuccess;
            jm.data = tpath + fileName;

            return jm;
        }
        #endregion

        #region 查询导出============================================================
        // POST: Api/CoreCmsChannel/QueryExportExcel/10
        /// <summary>
        /// 查询导出
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("查询导出")]
        public async Task<AdminUiCallBack> QueryExportExcel()
        {
            var jm = new AdminUiCallBack();

            var where = PredicateBuilder.True<CoreCmsChannel>();
                //查询筛选
			
			//id int
			var id = Request.Form["id"].FirstOrDefault().ObjectToInt(0);
            if (id > 0)
            {
                where = where.And(p => p.id == id);
            }
			//渠道名字 varchar
			var name = Request.Form["name"].FirstOrDefault();
            if (!string.IsNullOrEmpty(name))
            {
                where = where.And(p => p.name.Contains(name));
            }
			//接口 varchar
			var method = Request.Form["method"].FirstOrDefault();
            if (!string.IsNullOrEmpty(method))
            {
                where = where.And(p => p.method.Contains(method));
            }
			//应用ID varchar
			var app_id = Request.Form["app_id"].FirstOrDefault();
            if (!string.IsNullOrEmpty(app_id))
            {
                where = where.And(p => p.app_id.Contains(app_id));
            }
			//sign varchar
			var sign = Request.Form["sign"].FirstOrDefault();
            if (!string.IsNullOrEmpty(sign))
            {
                where = where.And(p => p.sign.Contains(sign));
            }
			//secret varchar
			var secret = Request.Form["secret"].FirstOrDefault();
            if (!string.IsNullOrEmpty(secret))
            {
                where = where.And(p => p.secret.Contains(secret));
            }
			//创建时间 datetime
			var createtime = Request.Form["createtime"].FirstOrDefault();
            if (!string.IsNullOrEmpty(createtime))
            {
                if (createtime.Contains("到"))
                {
                    var dts = createtime.Split("到");
                    var dtStart = dts[0].Trim().ObjectToDate();
                    where = where.And(p => p.createtime > dtStart);
                    var dtEnd = dts[1].Trim().ObjectToDate();
                    where = where.And(p => p.createtime < dtEnd);
                }
                else
                {
                    var dt = createtime.ObjectToDate();
                    where = where.And(p => p.createtime > dt);
                }
            }
			//订单创建起始时间 datetime
			var create_time_start = Request.Form["create_time_start"].FirstOrDefault();
            if (!string.IsNullOrEmpty(create_time_start))
            {
                if (create_time_start.Contains("到"))
                {
                    var dts = create_time_start.Split("到");
                    var dtStart = dts[0].Trim().ObjectToDate();
                    where = where.And(p => p.create_time_start > dtStart);
                    var dtEnd = dts[1].Trim().ObjectToDate();
                    where = where.And(p => p.create_time_start < dtEnd);
                }
                else
                {
                    var dt = create_time_start.ObjectToDate();
                    where = where.And(p => p.create_time_start > dt);
                }
            }
			//订单创建截止时间 datetime
			var create_time_end = Request.Form["create_time_end"].FirstOrDefault();
            if (!string.IsNullOrEmpty(create_time_end))
            {
                if (create_time_end.Contains("到"))
                {
                    var dts = create_time_end.Split("到");
                    var dtStart = dts[0].Trim().ObjectToDate();
                    where = where.And(p => p.create_time_end > dtStart);
                    var dtEnd = dts[1].Trim().ObjectToDate();
                    where = where.And(p => p.create_time_end < dtEnd);
                }
                else
                {
                    var dt = create_time_end.ObjectToDate();
                    where = where.And(p => p.create_time_end > dt);
                }
            }
			//订单更新起始时间 datetime
			var update_time_start = Request.Form["update_time_start"].FirstOrDefault();
            if (!string.IsNullOrEmpty(update_time_start))
            {
                if (update_time_start.Contains("到"))
                {
                    var dts = update_time_start.Split("到");
                    var dtStart = dts[0].Trim().ObjectToDate();
                    where = where.And(p => p.update_time_start > dtStart);
                    var dtEnd = dts[1].Trim().ObjectToDate();
                    where = where.And(p => p.update_time_start < dtEnd);
                }
                else
                {
                    var dt = update_time_start.ObjectToDate();
                    where = where.And(p => p.update_time_start > dt);
                }
            }
			//订单更新截止时间 datetime
			var update_time_end = Request.Form["update_time_end"].FirstOrDefault();
            if (!string.IsNullOrEmpty(update_time_end))
            {
                if (update_time_end.Contains("到"))
                {
                    var dts = update_time_end.Split("到");
                    var dtStart = dts[0].Trim().ObjectToDate();
                    where = where.And(p => p.update_time_end > dtStart);
                    var dtEnd = dts[1].Trim().ObjectToDate();
                    where = where.And(p => p.update_time_end < dtEnd);
                }
                else
                {
                    var dt = update_time_end.ObjectToDate();
                    where = where.And(p => p.update_time_end > dt);
                }
            }
			//订单状态 varchar
			var status = Request.Form["status"].FirstOrDefault();
            if (!string.IsNullOrEmpty(status))
            {
                where = where.And(p => p.status.Contains(status));
            }
			//是否存在售后 varchar
			var have_after_sale = Request.Form["have_after_sale"].FirstOrDefault();
            if (!string.IsNullOrEmpty(have_after_sale))
            {
                where = where.And(p => p.have_after_sale.Contains(have_after_sale));
            }
			//查询起点页数 int
			var next_id = Request.Form["next_id"].FirstOrDefault().ObjectToInt(0);
            if (next_id > 0)
            {
                where = where.And(p => p.next_id == next_id);
            }
			//条数 int
			var limit = Request.Form["limit"].FirstOrDefault().ObjectToInt(0);
            if (limit > 0)
            {
                where = where.And(p => p.limit == limit);
            }
			//平台ID int
			var platformId = Request.Form["platformId"].FirstOrDefault().ObjectToInt(0);
            if (platformId > 0)
            {
                where = where.And(p => p.platformId == platformId);
            }
            //获取数据
            //创建Excel文件的对象
            var book = new HSSFWorkbook();
            //添加一个sheet
            var mySheet = book.CreateSheet("Sheet1");
            //获取list数据
            var listModel = await _CoreCmsChannelServices.QueryListByClauseAsync(where, p => p.id, OrderByType.Asc, true);
            //给sheet1添加第一行的头部标题
                var headerRow = mySheet.CreateRow(0);
            var headerStyle = ExcelHelper.GetHeaderStyle(book);
            var cellNum = 0;
            
            var cell0 = headerRow.CreateCell(cellNum);
            cell0.SetCellValue("id");
            cell0.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;
			
            var cell1 = headerRow.CreateCell(cellNum);
            cell1.SetCellValue("渠道名字");
            cell1.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;
			
            var cell2 = headerRow.CreateCell(cellNum);
            cell2.SetCellValue("接口");
            cell2.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;
			
            var cell3 = headerRow.CreateCell(cellNum);
            cell3.SetCellValue("应用ID");
            cell3.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;
			
            var cell4 = headerRow.CreateCell(cellNum);
            cell4.SetCellValue("sign");
            cell4.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;
			
            var cell5 = headerRow.CreateCell(cellNum);
            cell5.SetCellValue("secret");
            cell5.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;
			
            var cell6 = headerRow.CreateCell(cellNum);
            cell6.SetCellValue("创建时间");
            cell6.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;
			
            var cell7 = headerRow.CreateCell(cellNum);
            cell7.SetCellValue("订单创建起始时间");
            cell7.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;
			
            var cell8 = headerRow.CreateCell(cellNum);
            cell8.SetCellValue("订单创建截止时间");
            cell8.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;
			
            var cell9 = headerRow.CreateCell(cellNum);
            cell9.SetCellValue("订单更新起始时间");
            cell9.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;
			
            var cell10 = headerRow.CreateCell(cellNum);
            cell10.SetCellValue("订单更新截止时间");
            cell10.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;
			
            var cell11 = headerRow.CreateCell(cellNum);
            cell11.SetCellValue("订单状态");
            cell11.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;
			
            var cell12 = headerRow.CreateCell(cellNum);
            cell12.SetCellValue("是否存在售后");
            cell12.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;
			
            var cell13 = headerRow.CreateCell(cellNum);
            cell13.SetCellValue("查询起点页数");
            cell13.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;
			
            var cell14 = headerRow.CreateCell(cellNum);
            cell14.SetCellValue("条数");
            cell14.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;
			
            var cell15 = headerRow.CreateCell(cellNum);
            cell15.SetCellValue("平台ID");
            cell15.CellStyle = headerStyle;
            mySheet.SetColumnWidth(cellNum, 10 * 256);
            cellNum++;
			

            headerRow.Height = 30 * 20;
            var commonCellStyle = ExcelHelper.GetCommonStyle(book);

            //将数据逐步写入sheet1各个行
            for (var i = 0; i < listModel.Count; i++)
            {
                var rowTemp = mySheet.CreateRow(i + 1);
                var cellNumTemp = 0;

            var rowTemp0 = rowTemp.CreateCell(cellNumTemp);
                rowTemp0.SetCellValue(listModel[i].id.ToString());
                rowTemp0.CellStyle = commonCellStyle;
                cellNumTemp++;

            var rowTemp1 = rowTemp.CreateCell(cellNumTemp);
                rowTemp1.SetCellValue(listModel[i].name.ToString());
                rowTemp1.CellStyle = commonCellStyle;
                cellNumTemp++;

            var rowTemp2 = rowTemp.CreateCell(cellNumTemp);
                rowTemp2.SetCellValue(listModel[i].method.ToString());
                rowTemp2.CellStyle = commonCellStyle;
                cellNumTemp++;

            var rowTemp3 = rowTemp.CreateCell(cellNumTemp);
                rowTemp3.SetCellValue(listModel[i].app_id.ToString());
                rowTemp3.CellStyle = commonCellStyle;
                cellNumTemp++;

            var rowTemp4 = rowTemp.CreateCell(cellNumTemp);
                rowTemp4.SetCellValue(listModel[i].sign.ToString());
                rowTemp4.CellStyle = commonCellStyle;
                cellNumTemp++;

            var rowTemp5 = rowTemp.CreateCell(cellNumTemp);
                rowTemp5.SetCellValue(listModel[i].secret.ToString());
                rowTemp5.CellStyle = commonCellStyle;
                cellNumTemp++;

            var rowTemp6 = rowTemp.CreateCell(cellNumTemp);
                rowTemp6.SetCellValue(listModel[i].createtime.ToString());
                rowTemp6.CellStyle = commonCellStyle;
                cellNumTemp++;

            var rowTemp7 = rowTemp.CreateCell(cellNumTemp);
                rowTemp7.SetCellValue(listModel[i].create_time_start.ToString());
                rowTemp7.CellStyle = commonCellStyle;
                cellNumTemp++;

            var rowTemp8 = rowTemp.CreateCell(cellNumTemp);
                rowTemp8.SetCellValue(listModel[i].create_time_end.ToString());
                rowTemp8.CellStyle = commonCellStyle;
                cellNumTemp++;

            var rowTemp9 = rowTemp.CreateCell(cellNumTemp);
                rowTemp9.SetCellValue(listModel[i].update_time_start.ToString());
                rowTemp9.CellStyle = commonCellStyle;
                cellNumTemp++;

            var rowTemp10 = rowTemp.CreateCell(cellNumTemp);
                rowTemp10.SetCellValue(listModel[i].update_time_end.ToString());
                rowTemp10.CellStyle = commonCellStyle;
                cellNumTemp++;

            var rowTemp11 = rowTemp.CreateCell(cellNumTemp);
                rowTemp11.SetCellValue(listModel[i].status.ToString());
                rowTemp11.CellStyle = commonCellStyle;
                cellNumTemp++;

            var rowTemp12 = rowTemp.CreateCell(cellNumTemp);
                rowTemp12.SetCellValue(listModel[i].have_after_sale.ToString());
                rowTemp12.CellStyle = commonCellStyle;
                cellNumTemp++;

            var rowTemp13 = rowTemp.CreateCell(cellNumTemp);
                rowTemp13.SetCellValue(listModel[i].next_id.ToString());
                rowTemp13.CellStyle = commonCellStyle;
                cellNumTemp++;

            var rowTemp14 = rowTemp.CreateCell(cellNumTemp);
                rowTemp14.SetCellValue(listModel[i].limit.ToString());
                rowTemp14.CellStyle = commonCellStyle;
                cellNumTemp++;

            var rowTemp15 = rowTemp.CreateCell(cellNumTemp);
                rowTemp15.SetCellValue(listModel[i].platformId.ToString());
                rowTemp15.CellStyle = commonCellStyle;
                cellNumTemp++;

            }
            // 写入到excel
            string webRootPath = _webHostEnvironment.WebRootPath;
            string tpath = "/files/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "-CoreCmsChannel导出(查询结果).xls";
            string filePath = webRootPath + tpath;
            DirectoryInfo di = new DirectoryInfo(filePath);
            if (!di.Exists)
            {
                di.Create();
            }
            FileStream fileHssf = new FileStream(filePath + fileName, FileMode.Create);
            book.Write(fileHssf);
            fileHssf.Close();

            jm.code = 0;
            jm.msg = GlobalConstVars.ExcelExportSuccess;
            jm.data = tpath + fileName;

            return jm;
        }
        #endregion

        

    }
}
